package club.justwill.single.persistence.store;

import club.justwill.single.persistence.document.SkuSupport;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by luoyu on 2/20/16.
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class SkuSupoortStore extends MongoDbStore<SkuSupport>{
    public List<SkuSupport> searchSkuSupportByProject(Long projectId) {
        return mongoTemplate.find(Query.query(Criteria.where("project_id").is(projectId)),SkuSupport.class);
    }

    public SkuSupport searchSkuSupportBySkuId(long skuId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("sku_id").is(skuId)),SkuSupport.class);
    }

    public void updateSkuSupport(long skuId) {
        SkuSupport skuSupport = searchSkuSupportBySkuId(skuId);
        skuSupport.setPeopleNum(skuSupport.getPeopleNum()+1);
        mongoTemplate.save(skuSupport);
    }
}
